import edu.princeton.cs.algs4.StdDraw
import edu.princeton.cs.algs4.StdRandom
import org.junit.Test

/**
 * 作者：Monkey
 * 日期：2017/11/5
 */
class StdTest {
    @Test
    fun test1() {
        val n = 100
        StdDraw.setXscale(0.0, n.toDouble())
        StdDraw.setYscale(0.0, n * n.toDouble())
        StdDraw.setPenRadius(.01)
        (0..n).map {
            val i = it.toDouble()
            StdDraw.point(i, i)
            StdDraw.point(i, i * i)
            StdDraw.point(i, i * Math.log(i))
        }
        readLine()
    }

    @Test
    fun test2() {
        val n = 50
        val arr = Array(n) {
            StdRandom.uniform()
        }
        drawRectangle(arr)
    }

    @Test
    fun test3() {
        val n = 50
        var arr = Array(n) {
            StdRandom.uniform()
        }
        arr = arr.sortedArray()
        drawRectangle(arr)
    }

    private fun drawRectangle(arr: Array<Double>) {
        val n = arr.size
        arr.withIndex().forEach {
            val i = it.index.toDouble()
            val value = it.value
            val x = i / n
            val y = value / 2
            val rw = 0.4 / n
            val rh = y
            StdDraw.filledRectangle(x, y, rw, rh)
            Thread.sleep(100)
        }
    }
}